<template>
  <van-picker
    title="更新性别"
    :default-index="gender"
    show-toolbar
    :columns="columns"
    @confirm="onConfirm"
    @cancel="onCancel"
  />
</template>

<script>
import { editUserProfileAPI } from "@/api";
export default {
  name: "updategender",
  props: {
    gender: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      columns: ["男", "女"]
    };
  },
  methods: {
    async onConfirm(value, index) {
      try {
        this.$toast.loading({
          message: "修改性别中...",
          forbidClick: true,
          duration: 0
        });
        // 1.发请求
        await editUserProfileAPI({
          gender: index
        });

        // 2.更新试图
        this.$emit("update:gender", index);

        // 3.关闭弹层
        this.$emit("close");

        // 4.提示
        this.$toast.success("修改性别成功");
      } catch (error) {
        this.$toast.fail("修改性别失败");
      }
    },
    onCancel() {
      this.$emit("close");
    }
  }
};
</script>

<style></style>
